let http = require("http");
let crypto = require("crypto");
const { spawn } = require("child_process");
const sendMail = require("./sendMail");
let SECRET = "123456";
function sign(body) {
  return `sha1=` + crypto.createHmac("sha1", SECRET).update(body).digest("hex");
}
let server = http.createServer(function (req, res) {
  if (req.url === "/webhook") {
    let buffers = [];
    req.on("data", function (buffer) {
      buffers.push(buffer);
    });
    req.on("end", function (buffer) {
      let body = Buffer.concat(buffers).toString();
      let event = req.headers["x-gitee-event"];
      let signature = req.headers["x-gitee-token"];
      console.log("接受到gitee的请求");
      console.log("event:" + event);
      console.log("signature:" + signature);
      console.log("body:" + body);
      // if (signature !== sign(body)) {
      //   return res.end("not allowed");
      // }
      res.setHeader("Content-Type", "application/json");
      res.end(JSON.stringify({ ok: true }));
    });
  } else {
    res.end("not found");
  }
});
server.listen(4000, () => {
  console.log("http://localhost:4000");
});
